package grp.test.controller;

import grp.test.common.Const;
import grp.test.common.ResponseCode;
import grp.test.common.ServerResponse;
import grp.test.pojo.Book;
import grp.test.pojo.User;
import grp.test.service.IReserveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/reserve/")

public class ReserveController {

    @Autowired
    IReserveService iReserveService;

    //预定图书
    @RequestMapping(value = "addReserveBook.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> addReserveBook(Integer bookId, HttpSession session){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null)
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
                    "请先登陆");
        return iReserveService.addReserveBook(user.getUserId(), bookId);


    }

    //取消预定
    @RequestMapping(value = "deleteReserveBook.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> cancelReserveBook(Integer bookId, HttpSession session){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
                    "请先登陆");
        }
        return iReserveService.cancelReserveBook(user.getUserId(),bookId);

    }

    //用户查看自己的预定图书列表
    @RequestMapping(value = "selectMyReserve.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<List<Book>> selectMyReserve(Integer userId){
        return iReserveService.selectMyReserve(userId);
    }
}
